package com.css.fxfzypg.modules.report.service.impl;


import com.css.fxfzypg.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzypg.base.attachment.repository.AttachmentInfoRepository;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.report.entity.ReportTemplateEntity;
import com.css.fxfzypg.modules.report.queryParams.ReportTemplateEntityQueryParams;
import com.css.fxfzypg.modules.report.repository.ReportTemplateRepository;
import com.css.fxfzypg.modules.report.service.ReportTemplateService;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-11-04 15:25
 */
@Service
@Slf4j
public class ReportTemplateServiceImpl implements ReportTemplateService {


    @Resource
    private ReportTemplateRepository reportTemplateRepository;

    @Resource
    private AttachmentInfoRepository attachmentInfoRepository;

    @Resource
    private SUserService sUserService;

    @Override
    @Transactional
    public Object saveReportTemplate(ReportTemplateEntity reportTemplateEntity) {
        reportTemplateEntity.setId(UUIDGenerator.getUUID());
        //当前登录人ID
        String userID = PlatformSessionContext.getUserID();
        reportTemplateEntity.setCreateUser(userID);
        //获取当前登录人名字
        String userJson = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        if (sUser != null) {

            reportTemplateEntity.setPepole(sUser.getUserName());
        }
        //上传时间
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        reportTemplateEntity.setRtTime(currentTimestamp);
        reportTemplateEntity.setCreateTime(currentTimestamp);
       //模板文件ID
        String attId = reportTemplateEntity.getAttachId();
        //查询模板文件信息 获取文件名字
        AttachmentInfoEntity attach = attachmentInfoRepository.getAttach(attId);
        reportTemplateEntity.setRtName(attach.getAttachName());
        reportTemplateEntity.setDelFlag(YNEnum.N.toString());
        return reportTemplateRepository.saveReportTemplate(reportTemplateEntity);
    }

    @Override
    public Map<String, Object> getAllReportTemplate(ReportTemplateEntityQueryParams reportTemplateEntityQueryParams) {
        return reportTemplateRepository.getAllReportTemplate(reportTemplateEntityQueryParams);
    }

    @Override
    @Transactional
    public void delReportTemplate(List<String> ids) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        reportTemplateRepository.delReportTemplate(ids,userID,timestamp);
    }

    @Override
    public RestResponse getReportTemplateByType(String rtType) {
        try {
            return RestResponse.succeed(reportTemplateRepository.getReportTemplateByType(rtType));
        } catch (Exception e) {
            log.error("查询报告模板信息失败!", e);
            return RestResponse.fail("查询报告模板信息失败");
        }
    }
}
